/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj (url) {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse(
    '{"' +
      decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"')
        .replace(/\+/g, ' ') +
      '"}'
  )
}

export function deepClone (source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments', 'deepClone')
  }
  const targetObj = source.constructor === Array ? [] : {}
  Object.keys(source).forEach(keys => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys])
    } else {
      targetObj[keys] = source[keys]
    }
  })
  return targetObj
}


export function obj2FormData(obj){
  let form = new FormData();
  if(obj){
    let keys = Object.keys(obj)
    keys.forEach(key => {
      form.append(key, obj[key])
    })
  }
  return form ;
}

     /**
      * 时间戳转日期
      * 1：格式y-M-d h:m:s 
      * 2：格式y-M-d
      * 3：格式y.M.d
      * 4：格式y-M-d h:m:s 
      * 5：格式M-d h:m
      * 6：如果为今年格式M-d h:m否则y-M-d h:m
      * 7：格式h:m:s 
      * 8：格式y-M-d h:m 
      * 9：格式y-M
      */
     export function	getLocalTime(time, num) {
       if(!time){
         return "";
       }
        
      time = parseFloat('' + time);

      let currentData = new Date();
      let date = new Date(time);
      let y = date.getFullYear();
      let month = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1);
      let d = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      let h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      let m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      let s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      if (num == 1) {
          return y + "-" + month + "-" + d + " " + h + ":" + m + ":" + s;
      } else if (num == 2) {
          return y + "-" + month + "-" + d;
      } else if (num == 3) {
          return y + "." + month + "." + d;
      } else if (num == 4) {
          return y + "." + month + "." + d + " " + h + ":" + m + ":" + s;
      } else if (num == 5) {
          return month + "-" + d + " " + h + ":" + m;
      } else if (num == 6) {
          return y == currentData.getFullYear() ? month + "-" + d + " " + h + ":" + m : y + "-" + month + "-" + d + " " + h + ":" + m;
      } else if (num == 7) {
          return h + ":" + m + ":" + s;
      } else if (num == 8) {
          return y + "-" + month + "-" + d + " " + h + ":" + m;
      } else if (num == 9) {
          return y + "-" + month;
      } else if (num == 10) {
          return y + "-" + month + "-" + d + " " + h + ":" + m;
      } else if (num == 11) {
          return month + "-" + d;
      } else if (num == 12) {
          return y + "-" + month + "-" + d + " " + h + ":" + m
      } else if (num == 13) {
          return month + "/" + d + " " + h + ":" + m
      } else if (num == 14) {
          return y + "年" + month + "月" + d + "日" + "下午 12:00 - 17:00"
      } else if (num == 15) {
          return y + month + d;
      } else if (num == 16) {
          return y + '/' + month;
      } else if (num == 17) {
          return y + '/' + month + '/' + d;
      } else if (num == 18) {
          return y + "年" + month + "月"
      } else if (num == 19) {
          return y + '-' + month + '-' + d;
      } else if (num == 20) {
          return y + "年" + month + "月" + d + "日" + h + ":" + m;
      } else if (num == 22) {
          return d;
      } else if (num == 23) {
          return h + ":" + m ;
      } else if (num == 24) {
        return y;
    }
  }

  
export function uuid() {
  let s = [];
  let hexDigits = "0123456789abcdef";
  for (let i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = "-";

  let uuid = s.join("").replace(new RegExp(/(-)/g), '');
  return uuid;
}



  //查询昨天的开始和结束时间
  export function  getYesterday(){
    var res = {
      startTime: '',
      endTime: ''
    };
    res.startTime = dateFtt(new Date(new Date().setDate(new Date().getDate() - 1)),"yyyy-MM-dd hh:mm:ss");
    res.endTime = dateFtt(new Date(new Date().setDate(new Date().getDate() - 1)),"yyyy-MM-dd hh:mm:ss")
  
    return res;
  }
  //   查询上周的开始和结束时间
    export function lastWeek(){
    var res = {
      startTime: '',
      endTime: ''
    };
    var now = new Date();
    var nowYear = now.getFullYear();//当前年
    var nowDayOfWeek = now.getDay(); //今天本周的第几天
    var nowDay = now.getDate(); //当前日
    var nowMonth = now.getMonth(); //当前月
  
    var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 6);
    var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
  
    res.startTime = dateFtt(weekStartDate,"yyyy-MM-dd hh:mm:ss");
    res.endTime = dateFtt(weekEndDate, "yyyy-MM-dd hh:mm:ss");
    return res ;
    }
  

    
//获取当前日期是星期几 ， 但是此处特殊处理了，当星期天的时候，等于7
export function weekDay(date){
	var ds = date.split("-");
    var dt = new Date(ds[0], ds[1]-1,ds[2]);
    var re = dt.getDay();
	if(!re)re = 7 ; //处理星期天
	return re ;
}

export function encodeHtml(str){
  var encodedStr = "" ;
  if (str=="") return encodedStr ;
  else {
      for (var i = 0 ; i < str.length ; i ++){
          encodedStr += "&#" + str.substring(i, i + 1).charCodeAt().toString(10) + ";" ;
      }
  }
  return encodedStr ;
}

//获取跟参考时间相差的日期
//showFormat 是getLocalTime 显示的类别
export function getDay(day, today = new Date(), showFormat=2){
  let targetday_milliseconds=today.getTime() + 1000*60*60*24*day;      
    return getLocalTime(targetday_milliseconds, showFormat);
}
//树形结构根据根节点 通过父子节点 添加一个属性给每一层
export   function changeTreePostionData(tree, id, changeTitle) {
  if (tree[0].nid === id) {
    // 根节点判定
    tree[0].ative = changeTitle;
    return tree;
  }
  const searchId = (source, id) => {
    // 开启递归
    for (let i = 0; i < source.length; i++) {
      const item = source[i];
      if (item.nid === id) {
        item.active = changeTitle;
        changeTreePostionData(tree, item.parentId, changeTitle);
      }
      if (item.child && item.child.length > 0) {
        // 只对非末端节点进行递归
        searchId(item.child, id);
      }
    }

  };
  searchId(tree[0].child, id);
  return tree;
}
// //  this.getTreePath(this.tableData, data=> data.date==='kafka',this.path)  用法
// 获取父节点组装成新数组
export  function  getTreePath(tree, func, path){
  if (!tree) return []
  for (const data of tree) {
    // 这里按照你的需求来存放最后返回的内容吧
    //这里的date是表示我是根据date去查询父级的，你可以修改成你的需求，其他不变
    path.push(data.date) 
    if (func(data)) return path
    if (data.children) {
      const findChildren = this.getTreePath(data.children, func, path)
      if (findChildren.length) return findChildren
    }
    path.pop()
  }
  return []
},
